new Vue({
    el: "#container",
    data() {
        return {
            tableSearch: {
                offset: 0,
                limit: 50,
                total: 0,
                dateFormat: "yyyy-MM-dd HH:mm",
                endTime: null,
                startTime: null,
                busName: "",
            },
            //  表数据
            tableDatas: [],
            tableLoading: false,
            //  通过弹窗
            adoptDialog: {
                show: false,
                loading: false,
            },
            positionList: [],
            //  通过表单
            adoptForm: {
                minusWeight: null,
                checkStatus: 1,
                uCode: null,
                position: null,
                positionName: null,
                spotCheck: null,
            }
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadTableDatas();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        searchDatas() {
            let that = this;
            that.tableSearch.offset = 0;
            that.tableSearch.limit = 50;
            that.loadTableDatas();
        },
        // 加载数据
        loadTableDatas: function () {
            let that = this;
            that.tableLoading = true;
            $.ajax({
                type: "POST",
                url: "/weight/order/wait/list",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let records = res.data;
                        if (records != null && records.length > 0) {
                            $.each(records, function (i, item) {
                                let imgUrl = item.imgUrl;
                                item.img = {
                                    pre: imgUrl,
                                    urls: [imgUrl],
                                };
                            })
                        }
                        that.tableDatas = records;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询过磅记录失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '查询过磅记录异常，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        showAdopt(data) {
            let that = this;
            //  加载仓位信息
            $.ajax({
                type: "POST",
                url: "/weight/order/storage",
                async: true,
                dataType: 'json',
                data: {
                    materialId: data.materialId,
                },
                success: function (res) {
                    if (res.code == 0) {
                        that.positionList = res.data;
                        that.adoptForm = {
                            minusWeight: null,
                            checkStatus: 1,
                            uCode: data.code,
                            spotCheck: data.spotCheck,
                            position: null,
                            positionName: null,
                        }
                        that.adoptDialog.show = true;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询仓位信息失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '查询仓位信息异常，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        /**
         * 通过
         */
        adopt() {
            let that = this;
            that.adoptDialog.loading = true;
            $.ajax({
                url: '/weight/order/examine/' + that.adoptForm.uCode,
                type: 'PUT',
                dataType: 'json',
                timeout: 30000,
                data: that.adoptForm,
                success: function (res) {
                    if (res.code == 0) {
                        // 设置数据
                        that.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        });
                        that.adoptDialog.show = false;
                        that.loadTableDatas();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '操作失败:' + res.msg,
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.adoptDialog.loading = false;
                    if (status == 'timeout') {
                        return;
                    }
                }
            })
        },
        /**
         * 审核数据,拒绝
         */
        refuse: function (code) {
            let that = this;
            if (isNull(code)) {
                that.$message({
                    showClose: true,
                    message: '未选择订单',
                    type: 'error'
                });
                return;
            }
            this.$prompt('请输入拒绝原因', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'danger',
                center: true
            }).then(({value}) => {
                that.tableLoading = true;
                $.ajax({
                    url: '/weight/order/examine/' + code,
                    type: 'PUT',
                    dataType: 'json',
                    timeout: 30000,
                    data: {
                        checkStatus: 2,
                        remarks: value,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            // 设置数据
                            that.$message({
                                showClose: true,
                                message: '操作成功',
                                type: 'success'
                            });
                            that.loadTableDatas();
                        } else {
                            that.$message({
                                showClose: true,
                                message: '审核失败:' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '审核异常',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                        that.tableLoading = false;
                        if (status == 'timeout') {
                            return;
                        }
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
    }
})
